<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/9/28
  Time: 14:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"  import="java.util.*,com.test.model.Employee" pageEncoding="UTF-8" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript">
        function myreset() {
            //通过id获取 到节点 ，将节点的value属性赋值为''
            document.getElementById("name").value="";
            document.getElementById("salary").value= '';
            document.getElementById("salary2").value= '';

        }
        function searchList() {
            //1.获取填写的参数
            var   url  = "/employee";
            var  str = "";
            //通过id获取节点的value属性的值
          var name = document.getElementById("name").value;
          var salary = document.getElementById("salary").value;
          var  salary2 =  document.getElementById("salary2").value;
            //2.发起请求
            if(name ||  salary ||  salary2){
                if (salary&&salary2){
                    if(parseInt(salary)>parseInt(salary2)){
                        alert("薪资开始值不能大于结束值");
                       return ;
                    }
                }

                if(name !=null){
                    //url+="?name="+name;
                    str  =  "?name="+name;
                }
                if(salary  !=  null){
                    // url+="&salary="+salary;
                    if(str.length>0){
                        str+="&salary="+salary;
                    }else{
                        str+="?salary="+salary;
                    }

                }
                if(salary2 != null){
                    if(str.length>0){
                        str+="&salary2="+salary2;
                    }else{
                        str+="?salary2="+salary2;
                    }
                }
            }
            //localhost:8080/employee?name=张三&salary=5000&salary2=1000&method=list
            window.location.href=url+str+'&method=list';

        }

        /**
         * 当页面加载成功立即请求servlet
         * @param ev
         */
        /*$(function(){
            //请求参数
            var list = {};
            //employee?name=zhangsan&salary=10000&salary2=5000    GET
            $.ajax({
                //请求方式
                type : "POST",
                //请求的媒体类型
                contentType: "application/json;charset=UTF-8",
                //请求地址
                url : "http://127.0.0.1/admin/list/",
                //数据，json字符串
                data:{
                    name:document.getElementById("name"),
                    salary:document.getElementById("salary"),
                    salary2:document.getElementById("salary2")
                },
                //data : JSON.stringify(list),
                //请求成功
                success : function(result) {
                   if(result !=null){
                       for(var i  = 0 ;i < result.length ; i++){
                          //var $tr =  $("#_table tbdoy").appendChild("<tr>");
                           var $tr = $("<tr></tr>");
                           $tr.append("<td>"+result[i].id+"</td>");
                           $tr.append("<td>"+result[i].name+"</td>");
                           $tr.append("<td>"+result[i].gender+"</td>");
                           $tr.append("<td>"+result[i].birthday+"</td>");
                           $tr.append("<td>"+result[i].email+"</td>");
                           $tr.append("<td>"+result[i].remark+"</td>");
                            $tr.appendTo("#_tbody");
                       }
                   }
                    console.log(result);
                },
                //请求失败，包含具体的错误信息
                error : function(e){
                    console.log(e.status);
                    console.log(e.responseText);
                }
            });
        });*/
        /**
         * 新增员工
         */
        function addEmployee() {
            //跳转新增页面
            window.location.href='/employee?method=toAddEmployeePage';
        }
    </script>
</head>
<body>
<div style="height:100px;">
    <div style="">
        <div style="float:left;">
            <span style="margin-left:150px;">姓名</span>
            <%
                String name = request.getParameter("name");
                if(name == null || name == ""){
                    name = "";
                }
                String salary = request.getParameter("salary");
                if(salary == null || salary == ""){
                    salary = "";
                }
                String salary2 = request.getParameter("salary2");
                if(salary2 == null || salary2 == ""){
                    salary2 = "";
                }
            %>
            <input id="name" style="width:150px;height:20px;" type="text" name="name" value="<%=name %>">
        </div>
        <div style="float:left;">
            <span style="margin-left:50px;">薪水</span>
            <input id="salary" style="width:120px;height:20px;" type="number" value="<%=salary %>" name="salary">&nbsp;至
            <input id="salary2" style="width:120px;height:20px;" type="number" value="<%=salary2 %>" name="salary2">
        </div>
    </div>
    <div style="margin-top:40px;" align="center">
        <input style="height:30px;font-size:16px;margin-right:20px;" type="button" onclick="searchList()" value="查询">
        <input style="height:30px;font-size:16px;margin-right:20px;" type="button" onclick="myreset()" value="重置">
        <input style="height:30px;font-size:16px;" type="button" onclick="addEmployee()" value="新增">
    </div>
</div>
<div align="center">
    <table onload="searchList()" cellpadding="0" cellspacing="0" border="1px black solid" id="_table">
        <thead>
        <tr style="height:40px;">
            <th style="width:80px;text-align: center;">序号</th>
            <th style="width:120px;text-align: center;">姓名</th>
            <th style="width:100px;text-align: center;">性别</th>
            <th style="width:150px;text-align: center;">出生日期</th>
            <th style="width:120px;text-align: center;">薪水</th>
            <th style="width:180px;text-align: center;">邮箱</th>
            <th style="width:150px;text-align: center;">备注</th>
            <th style="width:150px;text-align: center;">操作</th>
        </tr>
        </thead>
        <tbody id="_tbody">
        <%
            List<Employee> employees = (List<Employee>)request.getAttribute("employees");
            if(employees != null){
                for(int i=0; i<employees.size(); i++){
                    Employee e = employees.get(i);
        %>
        <tr style="height:35px;">
            <td style="text-align:center;"><%=i+1 %></td>
            <td style="text-align:center;"><a href="queryEmpInfo.do?id=<%=e.getId() %>" style="text-decoration:none;color:blue"><%=e.getName() %></a></td>
            <td style="text-align:center;"><%=e.getGender() %></td>
            <td style="text-align:center;"><%=e.getBirthday() %></td>
            <td style="text-align:center;"><%=e.getSalary() %></td>
            <td style="text-align:center;"><%=e.getEmail() %></td>
            <td style="text-align:center;"><%=e.getRemark() %></td>
            <td style="text-align:center;">
                <a href="openModifyEmpInfo.do?id=<%=e.getId()  %>" style="text-decoration:none;color:blue">修改</a>&nbsp;
                <a href="deleteEmp.do?id=<%=e.getId()  %>" style="text-decoration:none;color:blue">删除</a>
            </td>
        </tr>
        <%
                }
            }
        %>
        </tbody>
    </table>
</div>

</body>
</html>
